<template>
	<view>
		<view class="search-top">
			<view class="search-box">
				<uni-search-bar  @input="input" :radius="100" cancelButton="none" ></uni-search-bar>
			</view>
		</view>
		
		<!--搜索建议列表-->
		<view class="sugg-list" v-if="searchResult.length !== 0">
			<view class="sugg-item" v-for="(item,i) in searchResult" :key="i" @click="gotoDetail(item)">
				<view class="goods-name">{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="16" ></uni-icons>
			</view>
		</view>
		
		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<!-- 标题区域 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clean"></uni-icons>
			</view>
			
			<!-- 列表区域 -->
			<view class="history-list">
				<uni-tag :text="item" v-for="(item,i) in historys" :key="i" @click="gotoGoodsList(item)"></uni-tag>
			</view>
			
			
		</view>
	</view>
</template>

<script>
	export default {

		data() {
			return {
				timer: null,
				kw: '',
				searchResult: [],
				historyList:[]
			};
		},
		
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw')||'[]')
		},

		methods: {
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e
					this.getSearchList()
					this.saveSearchHistory()
				}, 500)
			},



			async getSearchList() {
				if (this.kw.length === 0) {
					this.searchResult = []
					return
				}

				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.kw
				})
				if (res.meta.status !== 200) return uni.$showMessage()
				this.searchResult = res.message
				// console.log(res.message)
			},
			
			gotoDetail(item){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?' + item.goods_id
				})
			},
			
			saveSearchHistory(){
				//2.1 直接把搜索关键词 push到historyList数组中
				// this.historyList.push(this.kw)
				
				//1.将array数组转化为Set对象
				const set = new Set(this.historyList)
				//2.调用set对象的delete方法，移除对应的元素
				set.delete(this.kw)
				//3.调用set对象add方法，向set中添加元素
				set.add(this.kw)
				//4.调用set对象转化为array数组
				this.historyList = Array.from(set)
				
				//持久化本地数据
				uni.setStorageSync('kw',JSON.stringify(this.historyList))

			},
			
			clean(){
				this.historyList = []
				uni.setStorageSync('kw','[]')
			},
			
			gotoGoodsList(item){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query=' + item
				})
			}
			
		},
		
		computed:{
			//计算属性
			historys(){
				//注意，由于数组是引用类型，所以不要直接基于原始数组调用reverse方法，以免修改原数组中元素的顺序
				return [...this.historyList].reverse()
			}
		}
	
	}
</script>

<style lang="scss">
	.search-box {
		//设置吸顶的‘位置’ 
		top: 0;
		//提高层级防止被轮播图覆盖
		z-index: 999;
		//设置定位效果为吸顶
		position: sticky;
	}

	.search-top {
		background-color: #C00000;
	}
	
	.sugg-list{
		padding: 0 5px;
		
		.sugg-item{
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #efefef;
			display: flex;
			align-items: center;
			justify-content: space-between;
			
			.goods-name{
				//文字不允许换行
				white-space: nowrap;
				//溢出部分隐藏
				overflow: hidden;
				//文本溢出后，使用...代替
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
		
	}
	
	
	.history-box{
		padding: 0 5px;
		
		.history-title{
			
			display: flex;
			justify-content: space-between;
			height: 40px;
			align-items: center;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		
		}
		
		.history-list{
			display: flex;
			flex-wrap: wrap;
			margin-top: 5px;
			
			.uni-tag{
				margin-top: 5px;
				margin-right: 5px;
				
			}
		}
	}
</style>
